<template>
	<view>
		<u-navbar title="煋玥阁" @leftClick="goBack()"></u-navbar>
		<view class="wrapper" v-if="id==='1'">
			<u-steps :current="curren">
				<u-steps-item title="填写号码"></u-steps-item>
				<u-steps-item title="验证邮箱"></u-steps-item>
				<u-steps-item title="重置密码"></u-steps-item>
			</u-steps>
			<view class="sept1">
				<u-form labelPosition="left" :model="userInfo" ref="uForm" label-width="80">
					<view v-if="curren===0">
						<u-form-item label="手机:" prop="userInfo.phone" borderBottom ref="item1">
							<u-input v-model="userInfo.phone" border="none" placeholder="请输入手机号"></u-input>
						</u-form-item>
					</view>
					<view v-if="curren===1">
						<u-form-item label="手机:" prop="userInfo.phone" borderBottom>
							<u-input v-model="userInfo.phone" border="none" disabled></u-input>
						</u-form-item>
						<u-form-item label="验证码:" prop="userInfo.code" borderBottom ref="item1">
							<u-input placeholder="验证码" v-model="userInfo.code" border="none">
								<template slot="suffix">
									<u-code ref="uCode" @change="codeChange" seconds="60" changeText="X秒重新获取"></u-code>
									<u-button @tap="getCode" :text="tips" type="success" size="mini"></u-button>
								</template>
							</u-input>
						</u-form-item>
					</view>
					<view v-if="curren===2">
						<u-form-item label="密码:" prop="userInfo.password" borderBottom>
							<u-input v-model="userInfo.passWord" type="password" border="none" placeholder="请输入密码">
							</u-input>
						</u-form-item>
						<u-form-item label="确认密码:" prop="userInfo.email" borderBottom>
							<u-input v-model="userInfo.rePassWord" type="password" border="none" placeholder="请确认密码">
							</u-input>
						</u-form-item>
					</view>

					<view style="display: flex;align-items: center;justify-content: space-between;margin-top: 150rpx; ">
						<u-button :type="preType" :disabled="predisabl" @click="pre()">{{pretitle}}</u-button>&nbsp;
						<u-button :type="bakType" :disabled="bakdisabl" @click="bak()">{{baktitle}}</u-button>
					</view>

				</u-form>
			</view>
		</view>

		<view class="wrapper" v-if="id==='2'">
			<u-steps :current="curren">
				<u-steps-item title="填写号码"></u-steps-item>
				<u-steps-item title="问题回答"></u-steps-item>
				<u-steps-item title="重置密码"></u-steps-item>
			</u-steps>
			<view class="sept1">
				<u-form labelPosition="left" :model="userInfo" ref="uForm" label-width="80">
					<view v-if="curren===0">
						<u-form-item label="手机:" prop="userInfo.phone" borderBottom ref="item1">
							<u-input v-model="userInfo.phone" border="none"></u-input>
						</u-form-item>
					</view>
					<view v-if="curren===1">
						<view style="margin-top: 30rpx;" v-for="(item,index) in wens2" :key="item.id">
							<text style="font-size: 15px;color: black;">{{item.wenName}}</text>
							<u-input v-model="wens2[index].daName" placeholder="请输入回答"></u-input>
						</view>

					</view>
					<view v-if="curren===2">
						<u-form-item label="密码:" prop="userInfo.password" borderBottom>
							<u-input v-model="userInfo.passWord" type="password" border="none" placeholder="请输入密码">
							</u-input>
						</u-form-item>
						<u-form-item label="确认密码:" prop="userInfo.email" borderBottom>
							<u-input v-model="userInfo.rePassWord" type="password" border="none" placeholder="请确认密码">
							</u-input>
						</u-form-item>
					</view>

					<view style="display: flex;align-items: center;justify-content: space-between;margin-top: 150rpx; ">
						<u-button :type="preType" :disabled="predisabl" @click="pre()">{{pretitle}}</u-button>&nbsp;
						<u-button :type="bakType" :disabled="bakdisabl" @click="bak2()">{{baktitle}}</u-button>
					</view>

				</u-form>
			</view>
		</view>
	</view>

</template>

<script>
	import navbox from '../components/navbox/navbox.vue'
	export default {
		data() {
			return {
				id: '',
				wens: [],
				wens2: [{
						wenName: "您童年的绰号叫什末？",
						daName: ''
					},
					{
						wenName: "不能说的秘密？",
						daName: ''
					},
					{
						wenName: "不能忘记的事情？",
						daName: ''
					}
				],
				userInfo: {

					phone: '',
					code: '',
					passWord: '',
					rePassWord: '',
					wen1: '',
					wen2: '',
					wen3: ''
				},
				tips: '',
				value: '',
				curren: 0,
				preType: 'default',
				bakType: 'primary',
				predisabl: true,
				bakdisabl: false,
				pretitle: '',
				baktitle: '下一步'
			}

		},
		components: {
			"navbox": navbox
		},
		methods: {
			goBack() {
				uni.navigateBack()
			},
			async bak2() {
				var that = this;
				if (this.curren === 1) {
					if (this.wens[0].daName !== this.wens2[0].daName || this.wens[1].daName !== this.wens2[1].daName ||
						this.wens[1].daName !== this.wens2[1].daName) {
						uni.showToast({
							title: '回答错误，重新作答',
							icon: 'none'
						});
						return;
					}
					this.curren = 2

				} else {
					this.predisabl = false
					this.predisabl = false
					if (this.curren === 0) {
						if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(that.userInfo.phone)) {
							uni.showToast({
								title: '请输入正确手机号',
								icon: 'none'
							});
							return;
						}
						uni.request({
							url: this.$baseurl + "/wexinLogin/findWenda?phone=" + this.userInfo.phone,
							success(res) {
								this.wens = res.data.data
								console.log(res)
								if (res.data.code === 500) {
									uni.showToast({
										title: res.data.msg,
										icon: 'none'
									});
									return;
								}
							}
						})
						// const res = await this.$myRuquest({
						// 	url: "/wexinLogin/findWenda?phone=" + this.userInfo.phone
						// })

					}
					this.step3()
					this.curren = this.curren + 1
					this.pretitle = "上一步"
					this.preType = 'primary'
				}
			},



			pre() {
				if (this.curren <= 1) {
					this.predisabl = true
					this.curren = 0
					this.pretitle = ""
					this.preType = 'default'

				} else {
					this.curren = this.curren - 1
					this.predisabl = false
					this.bakdisabl = false
					this.baktitle = "下一步"
					this.bakType = 'primary'
				}
			},
			async bak() {
				var that = this;
				if (this.curren === 1) {
					if (this.userInfo.code === "") {
						uni.showToast({
							title: '不能为空',
							icon: 'none'
						});
						return;
					}
					uni.request({
						url: this.$baseurl + "/wexinLogin/checkCode?code=" + this.userInfo.code,
						success(res) {
							if (res.data.code !== 200) {
								uni.showToast({
									title: res.data.msg,
									icon: 'none'
								})
								return;
							}
						}
					})
					// const res = await this.$myRuquest({
					// 	url: "/wexinLogin/checkCode?code=" + this.userInfo.code
					// })

					this.curren = 2

				} else {
					this.predisabl = false
					this.predisabl = false
					if (this.curren === 0) {
						if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(that.userInfo.phone)) {
							uni.showToast({
								title: '请输入正确手机号',
								icon: 'none'
							});
							return;
						}
						uni.request({
							url: this.$baseurl + "/wexinLogin/checkUser?phone=" + this.userInfo.phone,
							success(res) {
								if (res.data.code !== 200) {
									uni.showToast({
										title: res.data.msg,
										icon: 'none'
									})
									return;
								}
							}
						})
						// const res = await this.$myRuquest({
						// 	url: "/wexinLogin/checkUser?phone=" + this.userInfo.phone
						// })

					}
					this.step3()
					this.curren = this.curren + 1
					this.pretitle = "上一步"
					this.preType = 'primary'
				}
			},
			async step3() {
				if (this.curren === 2) {
					if (!this.userInfo.passWord) {
						uni.showToast({
							title: '密码不能为空',
							icon: 'none'
						});
						return;
					}
					if (this.userInfo.passWord !== this.userInfo.rePassWord) {
						uni.showToast({
							title: '密码不一致',
							icon: 'none'
						});
						return;
					}
					const a = {
						passWord: this.userInfo.passWord,
						token: this.userInfo.phone
					}
					uni.request({
						url: this.$baseurl + "/wexinLogin/reSetPass",
						success(res) {
							uni.reLaunch({
								url: '/pages/login/login'
							})
							uni.showToast({
								title: res.data.code === 200 ? "修改成功" : "修改失败，请联系管理员",
								icon: 'none'
							});
						}
					})
					// const res = await this.$myRuquest({
					// 	url: "/wexinLogin/reSetPass",
					// 	method: "post",
					// 	data: a,
					// })


				}
			},
			codeChange(text) {
				this.tips = text;
			},
			async getCode() {
				if (this.$refs.uCode.canGetCode) {
					uni.request({
						url: this.$baseurl +  "/wexinLogin/sentEmail?phone=" + this.userInfo.phone
					})
					// const res = await this.$myRuquest({
					// 	url: "/wexinLogin/sentEmail?phone=" + this.userInfo.phone
					// })
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			change(e) {
				console.log('change', e);
			}
		},
		onLoad: function(option) { //option为object类型，会序列化上个页面传递的参数
			this.id = option.id
			console.log(this.id); //打印出上个页面传递的参数。
		}
	}
</script>

<style lang="scss" scoped>
	.wrapper {
		position: absolute;
		top: 200rpx;
		width: 100%;

	}

	.sept1 {
		font-size: 20px;
		color: #000000;
		margin-top: 80rpx;
		width: 80%;
		margin-left: 10%;

		.input {
			padding: 0 20rpx 0 120rpx;
			height: 90rpx;
			line-height: 90rpx;
			margin-bottom: 50rpx;
			background: #f8f7fc;
			border: 1px solid #e9e9e9;
			font-size: 28rpx;

		}
	}
</style>
